<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./../layui/css/layui.css" media="all">
    <script src="./../layui/layui.js"></script>
    <script src="./../js/jquery-1.8.3.min.js"></script>
    <script src="./../js/env.js"></script>
    <title>region</title>
    <style>
        .layui-btn-group {
            margin: 10px 10px;
        }
    </style>
</head>

<body>

<fieldset class="layui-elem-field layui-field-title">
    <legend>区域管理</legend>
</fieldset>
<div class="demoTable">
    <button type="button" class="layui-btn" style="margin-left: 10px;margin-bottom: 10px" onclick="add()">添加区域</button>
</div>
<table id="demo" lay-filter="test"></table>
</body>

</html>

<script type="text/html" id="barDemo">
    <button type="button" onclick="edit('{{d.id}}','{{d.areaName}}','{{d.longitude}}','{{d.dimensionality}}')"
            class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit" style="background: dodgerblue">编辑
    </button>
    <button type="button" onclick="deleteArea('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            lay-event="edit" style="background: red">删除
    </button>
</script>
<script>
    if (layui.sessionData('user').userInfo === undefined) {
        window.location.href = "../html/login.html"
    }
    layui.use('table', function () {
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#demo',
            id: "testReload",
            headers: {sessionId: getToken()}
            , url: abc() + '/admin/config/getAreaLatLog' //数据接口
            , cols: [[ //表头
                {field: 'id', title: 'id', sort: true, fixed: 'left', align: 'center'}
                , {field: 'areaName', title: '区域名字', align: 'center'}
                , {field: 'longitude', title: '区域经度', align: 'center'}
                , {field: 'dimensionality', title: '区域维度', align: 'center'}
                , {field: 'right', title: '操作', width: 150, toolbar: '#barDemo', align: 'center'}
            ]]
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , done: function (res, curr, count) {
                if (res.code === 100) {
                    layer.msg("登录失效，请重新登录")
                    window.location.href = "../html/login.html"
                    return;
                }
            }
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code !== 0) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg //解析提示文本
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "data": res.data //解析数据列表
                    };
                }

            }
        });

    });

    function add() {
        layer.open({
            type: 2,
            title: "新增",
            anim: 2,
            maxmin: true,
            content: "./add/area_add.html",//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            area: ['500px', '557px']
        });
    }

    function edit(id, areaName, lng, lat) {
        layer.open({
            type: 2,
            title: "新增",
            anim: 2,
            maxmin: true,
            content: "./edit/area_edit.html",//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            area: ['500px', '557px'],
            success: function (layero, index) {
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                iframeWin.$("#areaName").val(areaName)
                iframeWin.$("#lat").val(lat)
                iframeWin.$("#lng").val(lng)
                iframeWin.$("#latlng").val("经度：" + lng + "   维度：" + lat)
                iframeWin.$("#id").val(id)
            }
        });
    }

    function deleteArea(id) {
        layer.confirm('确定删除?', function (index) {
            var param = {};
            param.id = id;
            var respones = MyAjax("GET", "/admin/config/delAreaLatLog", param, getToken());
            if (respones.code === 0) {
                layer.msg("删除成功");
                //表刷新方法
                var table = layui.table;
                table.reload('testReload', {
                    url: abc() + '/admin/config/getAreaLatLog'
                });
            }
        });
    }
</script>